<?php

namespace app\admin\model;

use think\Model;


class ReceiveGift extends Model
{

    // 表名
    protected $name = 'receive_gift';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'cat_text',
        'status',
        'receive_type_name'
    ];
    

    
   public function getCatList()
    {
        return ['1' => __('Cat 1'), '2' => __('Cat 2'), '3' => __('Cat 3'),'4' => __('Cat 4'),'5' => __('Cat 5')];
    }
    public function getCatTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['cat']) ? $data['cat'] : '');
        $list = $this->getCatList();
        return array_search($value,$list) ;
    }
    public function getStatusAttr($value, $data)
    {
        switch ($value){
            case 0:
                $value = "未审核";
                break;
            case 1:
                $value = "已审核";
                break;
            case 2:
                $value = "审核失败";
                break;default:$value='未审核';
        }
        return $value;
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getReceiveTypeNameAttr($value, $data)
    {
        if(!empty($data['receive_type_id'])){
            $typeData =  ReceiveType::where(['id'=>$data['receive_type_id']])->field('type_name')->find();
            return $typeData['type_name'];
        }else{
            return $value;
        }
    }

    public function receivetype()
    {
        return $this->belongsTo('ReceiveType', 'receive_type_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
